package com.leyou.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.auth.entity.ClientInfo;
import com.leyou.auth.mapper.ClientMapper;
import com.leyou.auth.service.ClientService;
import com.leyou.common.exception.LyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class ClientServiceImpl extends ServiceImpl<ClientMapper, ClientInfo> implements ClientService {

    @Autowired
    private ClientService clientService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Value("${ly.jwt.key}")
    private String key;


    @Override
    public String getKey(String clientId, String secret) {
        ClientInfo clientInfo = clientService.getOne(new QueryWrapper<ClientInfo>().eq("client_id", clientId));
        if (clientInfo == null){
            throw new LyException(408, "不合法的clientId");
        }

        if(! passwordEncoder.matches(secret, clientInfo.getSecret())){
            throw new LyException(409, "clientId密匙不合法");
        }
        return key;
    }
}
